/*
 * @Description: 加密、解密
 * @Autor: xiej
 * @Date: 2020-07-14 10:10:51
 * @LastEditors: xiej
 * @LastEditTime: 2020-07-14 13:53:05
 */
import CryptoJS from 'crypto-js'

// 如果是32位在java后端解密会报错 AES获取Cipher异常：Illegal key size 16位就不会
// const key = CryptoJS.enc.Utf8.parse('61DC779D88F539478E45C55452AF9DBB')
const key = CryptoJS.enc.Utf8.parse('61DC779D88F53947')
const iv = CryptoJS.enc.Utf8.parse('1b76d0c6af4bcb56')

/**
 * @description: 加密
 * @param {type}
 * @return:
 */
export function encrypt(value) {
  const encrypted = CryptoJS.AES.encrypt(value, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  }).toString()
  return encrypted
}

/**
 * @description: 解密
 * @param {type}
 * @return:
 */
export function decrypt(value) {
  const decrypted = CryptoJS.AES.decrypt(value, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  }).toString(CryptoJS.enc.Utf8)
  return decrypted
}
